package edu.scau.divineplace.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import edu.scau.divineplace.entity.dos.MusicDiscListDO;
import edu.scau.divineplace.entity.dos.UserDO;
import edu.scau.divineplace.entity.enums.UserTypeEnum;
import edu.scau.divineplace.entity.vo.music.MusicDiscListVO;
import edu.scau.divineplace.entity.vo.music.MusicDiscVO;
import edu.scau.divineplace.util.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2020/12/11 15:48
 */
@Service
public class MusicService {

    @Autowired
    private ImportMapperService importMapperService;

    /**
     * 通过内容id获取音乐的disc列表
     * @param contentId
     * @return
     */
    public List<MusicDiscListVO> getDiscList(Long contentId){
        // 先根据contentId拿到所有的disc
        LambdaQueryWrapper<MusicDiscListDO> discQuery = Wrappers.<MusicDiscListDO>lambdaQuery()
                .select(MusicDiscListDO::getDisc)
                .eq(MusicDiscListDO::getContentId, contentId)
                .groupBy(MusicDiscListDO::getDisc)
                .orderByAsc(MusicDiscListDO::getDisc);
        List<MusicDiscListDO> discDOS = importMapperService.musicDiscListMapper.selectList(discQuery);
        List<MusicDiscListVO> VOS = Lists.newArrayList();
        // 根据disc和contentId去拿具体的歌
        for (MusicDiscListDO discDO : discDOS) {
            Long disc = discDO.getDisc();
            LambdaQueryWrapper<MusicDiscListDO> songQuery = Wrappers.<MusicDiscListDO>lambdaQuery()
                    .eq(MusicDiscListDO::getContentId, contentId)
                    .eq(MusicDiscListDO::getDisc, disc)
                    .orderByAsc(MusicDiscListDO::getUpdateTime)
                    .orderByAsc(MusicDiscListDO::getCreateTime);
            List<MusicDiscListDO> songDOS = importMapperService.musicDiscListMapper.selectList(songQuery);
            List<MusicDiscVO> songVOS = BeanConvertUtils.DOs2BOs(songDOS, MusicDiscVO.class);
            VOS.add(MusicDiscListVO
                    .builder()
                    .disc(disc)
                    .musicDiscList(songVOS)
                    .build()
            );
        }
        return VOS;
    }

}
